package com.service.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.user.pojo.UcenterMember;
import com.service.user.mapper.UcenterMemberMapper;
import com.service.user.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.utils.MD5;
import com.service.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.transform.Result;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-09-30
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;

    @Override
    public ResultVo pages(Integer pageNum, Integer rows, UcenterMember ucenterMember) {
        //初始化构造查询
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        //分页
        Page<UcenterMember> page = new Page<>(pageNum,rows);
        //判断查询条件是否为空
        if (ucenterMember==null){
            this.page(page,null);
        }else{
            //判断查询条件的呢称是否为空 如果不为空就添加模糊查询
            if (!StringUtils.isEmpty(ucenterMember.getNickname())){
                queryWrapper.like("nickname",ucenterMember.getNickname());
            }
            if (!StringUtils.isEmpty(ucenterMember.getSex())){
                queryWrapper.eq("sex",ucenterMember.getSex());
            }
            this.page(page,queryWrapper);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("pageNum",page.getCurrent());
        map.put("items",page.getRecords());
        return ResultVo.ok().data(map);

    }

    @Override
    public ResultVo login(String mobile,String password) {
        //通过手机号查询用户信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",mobile);
        UcenterMember one = getOne(queryWrapper);
        //如果查询结果为空 就代表手机号输入错误

        if (one==null){
            return ResultVo.error().message("手机号码输入错误！！");
        }
        if (one.getIsDisabled()){
            return ResultVo.error().message("该账号已被禁用,请联系管理员！！");
        }
        //判断密码是否正确
        if (!one.getPassword().equals(MD5.encrypt(password))){
            return ResultVo.error().message("密码输入错误！！");
        }
        return ResultVo.ok().data("item",one);
    }

    @Override
    public ResultVo verifyMobile(String mobile) {
        //判断手机号是否可用
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",mobile);
        UcenterMember one = getOne(queryWrapper);
        if (one==null){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @Override
    public ResultVo register(UcenterMember ucenterMember,String code) {
        //获取redis string类型对象
        ValueOperations<String, String> string = stringRedisTemplate.opsForValue();
        //获取存在redis中的验证码
        String s = string.get("guli:register:" + ucenterMember.getMobile());
        //判断生成的验证码和用户输入的验证码是否相同
        if (!s.equals(code)){
            return ResultVo.error().message("验证码输入错误！！");
        }
        //给用户信息进行初始化
        ucenterMember.setGmtModified(new Date());
        ucenterMember.setGmtCreate(new Date());
        ucenterMember.setPassword(MD5.encrypt(ucenterMember.getPassword()));
        //添加到数据库中
        save(ucenterMember);
        return ResultVo.ok();
    }

    @Override
    public Integer queryRegisterCount(String day) {

        return ucenterMemberMapper.queryRegisterCount(day);
    }
}
